package ru.niir.goodfil.audit.domain;

import ru.niir.goodfil.audit.util.Assert;

import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
public class ExceptionEvent extends Event {
    @Column(length = 100)
    private String exception;

    @Column(length = 5000)
    private String stacktrace;

    private ExceptionEvent() {
    }

    public ExceptionEvent(String login, String ip, String exception, String stacktrace) {
        Assert.notBlank(exception);
        Assert.notBlank(stacktrace);
        Assert.notBlank(login);
        Assert.notBlank(ip);

        //  Trim exception
        if (exception.length() > 100) {
            exception = exception.substring(0, 100);
        }
        //  Trim stacktrace
        if (stacktrace.length() > 5000) {
            stacktrace = stacktrace.substring(0, 5000);
        }

        this.exception = exception;
        this.stacktrace = stacktrace;

        this.login = login;
        this.ip = ip;
    }

    public String getException() {
        return exception;
    }

    public void setException(String exception) {
        this.exception = exception;
    }

    public String getStacktrace() {
        return stacktrace;
    }

    public void setStacktrace(String stacktrace) {
        this.stacktrace = stacktrace;
    }

    @Override
    public String toString() {
        return "ExceptionEvent{" +
                "exception='" + exception + '\'' +
                ", stacktrace='" + stacktrace + '\'' +
                '}';
    }
}
